# coding:utf-8
from enum import Enum

from PyQt5.QtCore import QFile
from PyQt5.QtWidgets import QWidget


class Theme(Enum):
    DARK = 'dark'
    LIGHT = 'light'


def getStyleSheet(file: str, theme: Theme) -> str:
    filePath = f":/qss/{theme.value.lower()}/{file}.qss"  # 正确路径格式
    print(f"Loading QSS: {filePath}")
    f = QFile(filePath)
    if not f.exists():  # 先检查文件是否存在
        raise FileNotFoundError(f"{filePath} not found")

    if not f.open(QFile.ReadOnly | QFile.Text):
        raise IOError(f"Cannot open {filePath}")

    data = f.readAll()
    qss = bytes(data).decode("utf-8")  # 更健壮的字节转字符串
    f.close()
    return qss

def setStyleSheet(widget: QWidget, file: str, theme=Theme.DARK):
    """ set the style sheet of widget

    Parameters
    ----------
    widget: QWidget
        the widget to set style sheet

    file: str
        qss file name, without `.qss` suffix

    theme: Theme
        the theme of style sheet
    """
    widget.setStyleSheet(getStyleSheet(file, theme))
